//
//  LGAdviceMedicineCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/6/16.
//

#import "LGAdviceMedicineCell.h"

@interface LGAdviceMedicineCell()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *remarkLabel;
/**  已停药 */
@property (nonatomic , strong)UILabel *stopLabel;
/**   */
@property (nonatomic , strong)UIImageView *tipView;

@end

@implementation LGAdviceMedicineCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}

-(void)setType:(NSString *)type{
    _type = type;
    if([type isEqualToString:@"top"]){
        //第一个
        self.baseView.layer.cornerRadius = 15;
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(viewPix(16));
        }];
        [self.stopLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView).offset(-viewPix(6));
        }];
        [self.baseView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(viewPix(4));
            make.bottom.equalTo(self.contentView).offset(viewPix(16));
        }];
        [self.tipView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView).offset(viewPix(5));
        }];
    }else if ([type isEqualToString:@"middle"]){
        //中间
        self.baseView.layer.cornerRadius = 0;
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(viewPix(6));
            make.bottom.equalTo(self.contentView).offset(-viewPix(10));
        }];
        [self.stopLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView).offset(-viewPix(6));
        }];
        [self.baseView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(-viewPix(16));
            make.bottom.equalTo(self.contentView).offset(viewPix(16));
        }];
        [self.tipView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView).offset(0);
        }];
    }else if ([type isEqualToString:@"bottom"]){
        //底部
        self.baseView.layer.cornerRadius = 15;
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(viewPix(6));
        }];
        [self.stopLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView).offset(-viewPix(16));
        }];
        [self.baseView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(-viewPix(16));
            make.bottom.equalTo(self.contentView).offset(-viewPix(4));
        }];
        [self.tipView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView).offset(-viewPix(5));
        }];
    }else{
        //只有一个
        self.baseView.layer.cornerRadius = 15;
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(viewPix(16));
        }];
        [self.stopLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView).offset(-viewPix(16));
        }];
        [self.baseView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(viewPix(4));
            make.bottom.equalTo(self.contentView).offset(-viewPix(4));
        }];
        [self.tipView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView).offset(0);
        }];
    }
}

-(void)setSourceType:(NSString *)sourceType{
    _sourceType = sourceType;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.titleLabel.text = [NSString stringWithFormat:@"%@，%@%@，%@，%@",dataDic[@"title"],dataDic[@"dose"],dataDic[@"dose_unit"],dataDic[@"take_method"],dataDic[@"take_frequency"]];
    
    NSString *remark = LGNSString(dataDic[@"memo"]);
    self.remarkLabel.text = (remark.length>0)?[NSString stringWithFormat:@"备注：%@",remark]:@"";
    CGFloat remarkTop = (remark.length>0)?viewPix(8):0;
    [self.remarkLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(remarkTop);
    }];
    
    NSString *state = LGNSString(dataDic[@"state"]); //10-新增 、 20-调整 、 30-停药
    BOOL isStop = [state isEqualToString:@"30"]?YES:NO;
    NSString *titleColor = (isStop == NO)?@"#666666":@"#DD3030";
    NSString *remarkColor = (isStop == NO)?@"#999999":@"#DD3030";
    self.titleLabel.textColor = [UIColor colorWithString:titleColor];
    self.remarkLabel.textColor = [UIColor colorWithString:remarkColor];
    self.stopLabel.text = (isStop == YES)?@"已停药":@"";
    CGFloat stopTop = (isStop == YES)?viewPix(8):0;
    [self.stopLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.remarkLabel.mas_bottom).offset(stopTop);
    }];
}


#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.clipsToBounds = YES;
    self.backgroundColor = [UIColor clearColor];
    self.contentView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.baseView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.remarkLabel];
    [self.contentView addSubview:self.stopLabel];
    [self.contentView addSubview:self.tipView];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(viewPix(16));
        make.right.equalTo(self.contentView).offset(-viewPix(16));
        make.top.equalTo(self.contentView).offset(viewPix(4));
        make.bottom.equalTo(self.contentView).offset(viewPix(4));
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(20));
        make.left.equalTo(self.contentView).offset(viewPix(28));
        make.right.equalTo(self.contentView).offset(-viewPix(44));
    }];
    [self.remarkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(viewPix(8));
        make.left.right.equalTo(self.titleLabel);
    }];
    [self.stopLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.remarkLabel.mas_bottom).offset(viewPix(8));
        make.left.right.equalTo(self.titleLabel);
        make.bottom.equalTo(self.contentView).offset(-viewPix(6));
    }];
    [self.tipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-viewPix(28));
        make.centerY.equalTo(self.contentView);
        make.width.height.equalTo(@(viewPix(12)));
    }];
}
-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc] init];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.cornerRidus = 15;
    }
    return _baseView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _titleLabel;
}

-(UILabel *)remarkLabel{
    if(!_remarkLabel){
        _remarkLabel = [UILabel lableWithText:@"" colorString:@"#999999" textFont:LGFont(13) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _remarkLabel;
}

-(UILabel *)stopLabel{
    if(!_stopLabel){
        _stopLabel = [UILabel lableWithText:@"已停药" colorString:@"#EB5757" textFont:LGFont(13) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _stopLabel;
}

-(UIImageView *)tipView{
    if(!_tipView){
        _tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip12"]];
    }
    return _tipView;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
